#!/bin/bash

set -e
sudo -v

EII_BUILD_DIR=$PWD/code/IEdgeInsights/build

display_progress () {
    echo " "
    echo "-----------------------------------------------"
    echo $1
    echo "-----------------------------------------------"
    echo " "
}

check_docker () {
    if [ -z $(which docker) ]; then
        echo "docker not install"
        exit 1
    fi
    if [ -z $(which docker-compose) ]; then
        echo "docker-compose not install"
        exit 1
    fi
}

run_pcb_demo () {
    # EII builder
    cd $EII_BUILD_DIR
    sudo -E python3 builder.py -f usecases/video-streaming.yml

    # Disable x server
    xhost +

    if [ -f "eii_start.sh" ]; then
	    echo "currect version is eii-3.0"
	    chmod +x eii_start.sh && ./eii_start.sh
    else
        echo "currect version is before eii-3.0"
	    # Provision
        cd provision
        sudo -E ./provision.sh ../docker-compose.yml
        # Run services
        cd ..
        docker-compose up -d
    fi

}

startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`

# Main Process
display_progress "Start to try eii pcb demo"
check_docker
run_pcb_demo
display_progress "Success to run eii pcb demo"

endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "$startTime ---> $endTime" "Total:$sumTime seconds"
echo " "
